/**
 * Definition for singly-linked list.
 * class ListNode {
 *     val: number
 *     next: ListNode | null
 *     constructor(val?: number, next?: ListNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */
  class ListNode {
      val: number
      next: ListNode | null
      constructor(val?: number, next?: ListNode | null) {
          this.val = (val===undefined ? 0 : val)
          this.next = (next===undefined ? null : next)
      }
  }

 function reverseList(head: ListNode | null): ListNode | null {
    let node = head;
    let before:ListNode|null = null;
    let next:ListNode|null = (node == null?null:node.next);
    if(next == null){
        return head;
    }
    while(node != null){
        node.next = before;
        
        before =node;
        node = next;
        next = (node == null?null:node.next);
    }
    return before;

};